package com.smart.base.tools.generator.util;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class TemplateUtil {

	public static void create(String ftlPackPath,String ftlName,String targetFile,Map params){
		try{
			Configuration cfg = new Configuration();
			cfg.setClassForTemplateLoading(TemplateUtil.class, ftlPackPath);
			cfg.setObjectWrapper(new DefaultObjectWrapper());
			Template tpl = cfg.getTemplate(ftlName);
			
			File file = new File(targetFile);
			File parentFile = file.getParentFile();
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			if (!file.exists()) {
				file.createNewFile();
			}
			Writer out = new FileWriter(file);
			tpl.process(params, out);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
